﻿
using Microsoft.AspNetCore.Mvc;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using rk310.BLL;
using rk310.MODEL;

namespace rk310.UI.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class FileController : ControllerBase
    {
        public FileController(Roombll roombll)
        {
            Roombll = roombll;
        }

        public Roombll Roombll { get; }

        [HttpGet("get")]
        public IActionResult get()
        {
            var list=Roombll.GetRoom();

            IWorkbook book=new XSSFWorkbook();
            var sheet = book.CreateSheet();

            var row =sheet.CreateRow(0);
            row.CreateCell(0).SetCellValue("编号");
            row.CreateCell(1).SetCellValue("用户名");
            row.CreateCell(2).SetCellValue("价格");
            row.CreateCell(3).SetCellValue("数量");
            row.CreateCell(4).SetCellValue("日期");
            row.CreateCell(5).SetCellValue("类型");

            int num = 1;

            foreach (var m in list)
            {
                var rowew = sheet.CreateRow(num);
                rowew.CreateCell(0).SetCellValue(m.id);
                rowew.CreateCell(1).SetCellValue(m.name);
                rowew.CreateCell(2).SetCellValue(m.price);
                rowew.CreateCell(3).SetCellValue(m.num);
                rowew.CreateCell(4).SetCellValue(m.date);
                rowew.CreateCell(5).SetCellValue(m.type);
                num++;
            }

            byte[] b;
            using(MemoryStream ms = new MemoryStream())
            {
                book.Write(ms);
                b=ms.ToArray();
            }

            return File(b, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "房号信息a.xlsx");


        }

        [HttpPost("dr")]
        public IActionResult dr(IFormFile file)
        {
            List<Room> rooms = new List<Room>();

            using(MemoryStream ms = new MemoryStream())
            {
                file.CopyTo(ms);
                ms.Position=0;
                IWorkbook book = new XSSFWorkbook(ms);
                var sheet1 = book.GetSheet("房号信息表");
                int num = sheet1.LastRowNum;
                for(int i = 1; i <= num; i++)
                {
                    var row=sheet1.GetRow(i);

                    Room m = new Room();

                    m.name = row.Cells[1].ToString();
                    m.price = row.Cells[2].ToString();
                    m.num = row.Cells[3].ToString();
                    m.date = row.Cells[4].ToString();
                    m.type = row.Cells[5].ToString();

                    rooms.Add(m);
                }
                

            }
            return Ok();
            



        }








    }
}


